package Jogo {
	import flash.geom.Point;
	import flash.utils.ByteArray;
	
	/** Class that contains some utility functions used
	 * by various classes.
	 * 
	 * @author Bruno Zumba
	 */
	public class Util {		
		/** The rotation that the object in pt1 must have so it
		 * faces pt2.
		 * 
		 * @return The angle, in degrees.
		 */ 
		public static function setRotation(pt1 : Point, pt2 : Point) : Number{			
			var alfa : Number;
			
			//Set the angle
			alfa = ((Math.atan(((pt2.y-pt1.y) / (pt2.x-pt1.x))))*180/Math.PI);
			
			/* Set some erros that appears from the formula above.
			* I guess they appear because the origin of the cartesian axis 
			* in flash is the top left corner. A.k.a. = POG (but it works)
			*/
			if (alfa == 0){
				if (pt2.x < pt1.x){
					alfa = 180;
				}
			} else if (alfa == 90){
				if (pt2.y < pt1.y){
					alfa = 270;
				}
			} else if (alfa < 0){
				if (pt2.y < pt1.y){
					alfa = 360-Math.abs(alfa);
				} else {
					alfa = 180-Math.abs(alfa);
				}
			} else {
				if (pt2.y < pt1.y){
					alfa +=180;
				}
				
			}
			//If the angle is < 0, then it's the complement of 360
			if (alfa < 0){
				alfa = 360+alfa;
			}
			if (alfa < 0){
				alfa = 360+alfa;
			}
			
			return alfa;
		}
		
		
	}
}